package com.his.system.manager.remote;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 路由Template
 *
 * @Description
 * @Author Admin
 * @Date 2020-08-08 20:44:14
 */
@Slf4j
@Service
public class RemoteTemplate {
    @Autowired
    private RestTemplate restTemplate;

    private String userIdentify = "44011700001000000002";
    private String domain = "http://58.62.133.214:80";
    private String doorInfosUrl = "/VIID/Community/DoorInfos";

    /**
     * 门禁数据
     *
     * @param jsonObject
     * @return
     */
    public JSONObject remoteDoorInfos(JSONObject jsonObject) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            headers.add("User-Identify", userIdentify);
            headers.add("Authorization",
                    "Digest " +
                            "username=\"hl\", " +
                            "realm=\"dngVIIDExt\", " +
                            "nonce=\"37b3be0f59844f83a6dc7b4d2ee9ee47\", " +
                            "uri=" + doorInfosUrl + ", " +
                            "qop=auth, " +
                            "nc=1, " +
                            "cnonce=\"1\", " +
                            "Password=\"123456\", " +
                            "response=\"02b6dc1a2e0d75a096dfdf7eb32707e8\", " +
                            "opaque=\"b5265cb228504c818a6406530d838573\""
            );
            //入参
            Map<String, Object> params = RestTemplateUtils.jsonToMap(jsonObject);
            String url = domain + doorInfosUrl;
            JSONObject res = RestTemplateUtils.post(restTemplate, url, headers, params);
            return res;
        } catch (Exception e) {
            log.error("运行异常!", e);
        }
        return new JSONObject();
    }
}
